package com.wz.fragment;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

import com.wz.activity.R;
import com.wz.bean.SendedMsg;
import com.wz.db.SmsProvider;
import com.wz.view.flowlayout.FlowLayout;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 
 * 显示历史短信数据
 */
public class SmsHistoryFragment extends ListFragment {

	private LayoutInflater minflater;
	private CursorAdapter mcursorAdapter;

	private static final int LOADERID = 1;

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		minflater = LayoutInflater.from(this.getActivity());

		initLoader();
		
		setupListAdapter();

		
	}

	private void setupListAdapter() {

		mcursorAdapter = new CursorAdapter(this.getActivity(), null, false) {

			@Override
			public View newView(Context context, Cursor cursor, ViewGroup parent) {

				View view = minflater.inflate(R.layout.item_sended_mesg, parent, false);

				return view;
			}

			@Override
			public void bindView(View view, Context context, Cursor cursor) {

				// 获取布局控件
				TextView tvmsg = (TextView) view.findViewById(R.id.wz_tv_msg);
				TextView tvfes = (TextView) view.findViewById(R.id.wz_tv_fes);
				TextView tvdate = (TextView) view.findViewById(R.id.wz_tv_date);
				FlowLayout flContacts = (FlowLayout) view.findViewById(R.id.wz_fl_contacts);

				// 填充数据
				// 短信内容
				tvmsg.setText(cursor.getString(cursor.getColumnIndex(SendedMsg.COLUMN_CONTENT)));
				// 节日
				tvfes.setText(cursor.getString(cursor.getColumnIndex(SendedMsg.COLUMN_FESTIVAL_NAME)));
				// 时间, 注意这里的date为long，int型会溢出
				long date = cursor.getLong(cursor.getColumnIndex(SendedMsg.COLUMN_DATE));
				tvdate.setText(parseDate(date));

				// 联系人名单,多个联系人用逗号分隔
				String names = cursor.getString(cursor.getColumnIndex(SendedMsg.COLUMN_NAMES));
				if (TextUtils.isEmpty(names)) {
					return;
				}

				// 因为ListView的item有复用的可能性，所以每次都要先除去item中的flContacts在上一次使用时添加的view
				flContacts.removeAllViews();

				for (String name : names.split(",")) {
					addTag(name, flContacts);
				}
			}

		};

		setListAdapter(mcursorAdapter);
	}

	private String parseDate(long date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CANADA);
		return df.format(date);
	}

	/**
	 * 向FlowLayout添加联系人
	 */
	private void addTag(String name, FlowLayout fl) {
		TextView tv = (TextView) minflater.inflate(R.layout.tag, fl, false);
		tv.setText(name);
		fl.addView(tv);
	}

	private void initLoader() {

		this.getLoaderManager().initLoader(LOADERID, null, new LoaderCallback(this));

	}

	/**
	 * 内部类
	 */
	public class LoaderCallback implements LoaderManager.LoaderCallbacks<Cursor> {

		SmsHistoryFragment fragment;

		public LoaderCallback(SmsHistoryFragment fragment) {
			this.fragment = fragment;
		}

		@Override
		public Loader<Cursor> onCreateLoader(int id, Bundle args) {

			CursorLoader loader = new CursorLoader(fragment.getActivity(), SmsProvider.URI_SMS_ALL, null, null, null, null);

			return loader;
		}

		@Override
		public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

			if (loader.getId() == LOADERID) {
				mcursorAdapter.swapCursor(data);
			}

		}

		@Override
		public void onLoaderReset(Loader<Cursor> loader) {
			mcursorAdapter.swapCursor(null);
		}

	}

}
